package com.origin.app.utils

import android.graphics.Color
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.origin.app.MyApplication
import com.origin.app.R
import com.origin.app.bean.CompanyPointBean
import com.origin.uilibrary.xbanner.XBannerUtils
import com.origin.utils.image.ImageLoader


object BindingAdapterUtils {
    @JvmStatic
    @BindingAdapter("dynamic_margin_start")
    fun dynamicMarginStart(viewGroup: ViewGroup, level: Int) {
        val lp = viewGroup.layoutParams as ViewGroup.MarginLayoutParams
        lp.leftMargin = XBannerUtils.dp2px(MyApplication.getInstance(),10*1.0f)*level
        viewGroup.setLayoutParams(lp)
    }

    @JvmStatic
    @BindingAdapter("item_select")
    fun itemSelect(imageView: ImageView, select: Boolean) {
        imageView.setImageResource(if(select) R.drawable.select_fill else R.drawable.svg_un_select)
    }

    @JvmStatic
    @BindingAdapter("padding_top_status_bar_height")
    fun paddingTopStatusBarHeight(view: View, boolean: Boolean) {
       view.setPadding(0, AppUtils.getStatusBarHeight(),0,0)
    }

    @JvmStatic
    @BindingAdapter("show_point_avatar")
    fun showPointAvatar(imageView: ImageView, companyPointBean: CompanyPointBean) {

        if(companyPointBean.picUrl==null||companyPointBean.picUrl.isEmpty()){
            imageView.setImageResource(R.mipmap.crim)
            return
        }
        try {

            ImageLoader.loaderImg(imageView.context,companyPointBean.picUrl,R.mipmap.crim,R.mipmap.crim).into(imageView)
        }catch (e:Exception){
            Log.e("加载的图片 x ", "url  ${companyPointBean.picUrl}")
        }

    }

    @JvmStatic
    @BindingAdapter("show_point_watch_app")
    fun showPointWatchApp(imageView: ImageView, companyPointBean: CompanyPointBean) {
        val isApp = companyPointBean.supplier == 6 || companyPointBean.supplier == 7;
        imageView.setImageResource(if(isApp){R.mipmap.type_app}else{R.mipmap.type_watch})
    }

    @JvmStatic
    @BindingAdapter("item_point_state")
    fun itemPointState(textView: TextView, companyPointBean: CompanyPointBean) {
      if (companyPointBean.equipmentState==1){
          textView.apply {
              text= "在线"
              setTextColor(Color.parseColor("#3BD66A"))
          }
      }else if (companyPointBean.equipmentState==2){
          textView.apply {
              text= "异常"
              setTextColor(Color.parseColor("#FF802C"))
          }
      }else if (companyPointBean.equipmentState==3){
          textView.apply {
              text= "离线"
              setTextColor(Color.parseColor("#999999"))
          }
      }else if (companyPointBean.equipmentState==4){
          textView.apply {
              text= "未上线"
             setTextColor(Color.parseColor("#FDD15B"))
          }
      }else{
          textView.text= "在线状态"
      }
    }
}